package top.infopub.mgr.hr.web;


import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.hr.service.StaffOfficeService;
import top.infopub.mgr.hr.vo.StaffInfoVo;
import top.infopub.mgr.hr.vo.StaffOfficeVo;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.tdp.util.GrpParaUtil;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;


/**
 * 人员管理
 * @author wangw
 * @version 2020年1月3日
 * @see StaffInfoController
 * @since
 */
@Controller
@RequestMapping("/staff")
public class StaffInfoController extends GenericController {

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private StaffOfficeService staffOfficeService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    /**
     * 人员列表
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        String bindOrgId = (String)SessionUserUtils.getSessionAttribute("bindOrgId");
        request.setAttribute("bindOrgId", bindOrgId);
        String visibleOrgIds = "";
        //String visibleOrgIds = (String)SessionUserUtils.getSessionAttribute("visibleOrgIds");
        List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if(StringUtils.isEmpty(visibleOrgIds)){
            Map<String, Object> where = Maps.newHashMap();
            where.put("state", 1);
            where.put("orderCondition", " types asc, add_time asc");
            orgInfos = orgInfoService.selectByMapWhere(where);
        }else{
            orgInfos = orgInfoService.selectByVisibleIds(visibleOrgIds);
        }
        request.setAttribute("orgInfos", orgInfos);
        return "hr/lay-staffInfo-list";
    }

    /**
     * 人员列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<StaffOfficeVo> queryDataByCondition(StaffOfficeVo staffOfficeVo, ParamPage paramPage) {
        Page<StaffOfficeVo> page = new Page<StaffOfficeVo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        if(StringUtils.isEmpty(staffOfficeVo.getOrgId())){
            Object tmp = SessionUserUtils.getSessionAttribute("bindOrgId");
            // 为空默认显示 总部
            if (tmp == null) {
                staffOfficeVo.setOrgId((String)SessionUserUtils.getSessionAttribute("superOrgId"));
            }
            else {
                staffOfficeVo.setOrgId((String)tmp);
            }
        }
        page = staffOfficeService.selectPageByDomainWhere(page, staffOfficeVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询人员详情（包含就职记录）
     * @param id 人员Id
     * @return
     * @see
     */
    @RequestMapping("/queryById")
    public @ResponseBody RspVo<StaffInfoVo> queryById(HttpServletRequest request){
        String id = request.getParameter("id");
        StaffInfoVo staffInfo = staffInfoService.selectById(id);
        return RspVo.getSuccessResponse(staffInfo);
    }

    /**
     * 新增/编辑人员
     * @param staffOfficeVo
     * @return
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @see
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/save")
    public @ResponseBody RspVo<StaffOfficeVo> save(StaffOfficeVo staffOfficeVo){
        if (StringUtils.isEmpty(staffOfficeVo.getStaffName())
            || StringUtils.isEmpty(staffOfficeVo.getStaffPhone())
            || StringUtils.isEmpty(staffOfficeVo.getOrgId())){
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
        }
        staffOfficeVo.setStaffName(staffOfficeVo.getStaffName().trim());
        staffOfficeService.saveStaffInfo(staffOfficeVo);
        if(StringUtils.isEmpty(staffOfficeVo.getId())){
            LogUtil.bizLog(LogModelType.OTHER, AcctionType.ADD, "新增人员 - 人员名称:" + staffOfficeVo.getStaffName().trim(), null);
        }else{
            LogUtil.bizLog(LogModelType.OTHER, AcctionType.UPDATE, "修改人员 - 人员Id:" + staffOfficeVo.getId(), null);
        }
        return RspVo.getSuccessResponse(staffOfficeVo);
    }

    /**
     * 人员就职信息列表数据
     * @return
     */
    @RequestMapping("/queryDataByStaffId")
    @ResponseBody
    public RspVo<StaffOfficeVo> queryDataByStaffId(StaffOfficeVo staffOfficeVo, ParamPage paramPage) {
        Page<StaffOfficeVo> page = new Page<StaffOfficeVo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = staffOfficeService.selectPageByDomainWhere(page, staffOfficeVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 新增/编辑人员就职信息
     * @param staffOfficeVo
     * @return
     * @see
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/saveOffice")
    public @ResponseBody RspVo<StaffOfficeVo> saveOffice(StaffOfficeVo staffOfficeVo){
        if (StringUtils.isEmpty(staffOfficeVo.getOrgId())
            || StringUtils.isEmpty(staffOfficeVo.getDeptId())
            || StringUtils.isEmpty(staffOfficeVo.getStaffId())
            || StringUtils.isEmpty(staffOfficeVo.getPostId())
            || staffOfficeVo.getStartTime() == null ){
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
        }
        staffOfficeService.saveStaffOffice(staffOfficeVo);
        if(StringUtils.isEmpty(staffOfficeVo.getId())){
            LogUtil.bizLog(LogModelType.OTHER, AcctionType.ADD, "新增人员 - 人员名称:" + staffOfficeVo.getStaffName().trim(), null);
        }else{
            LogUtil.bizLog(LogModelType.OTHER, AcctionType.UPDATE, "修改人员 - 人员Id:" + staffOfficeVo.getId(), null);
        }
        return RspVo.getSuccessResponse(staffOfficeVo);
    }

    /**
     * 修改人员状态
     * @param operatorVo
     * @return
     * @see
     */
    @RequestMapping("/updateStatus")
    public @ResponseBody RspVo<String> updateStatus(StaffInfo staffInfo) {
        StaffInfo staff = staffInfoService.selectByPrimaryKey(staffInfo.getId());
        Integer oldStatus = staff.getState();
        Integer newStatus = 0;
        if(oldStatus == 0){
            newStatus = 1;
        }else{
            newStatus = 0;
        }
        staff.setState(newStatus);
        staff.setUptTime(new Date());
        staff.setUptBy(SessionUserUtils.getSessionUserId());
        staffInfoService.updateBySelective(staff);
        // 操作日志
        LogUtil.bizLog(
            LogUtil.LogModelType.OTHER,
            LogUtil.AcctionType.UPDATE,
            "修改状态  人员姓名:" + staff.getStaffName() + " 原状态:"
                + GrpParaUtil.getName("A00010020", String.valueOf(oldStatus)) + " 被修改为:"
                + GrpParaUtil.getName("A00010020", String.valueOf(newStatus)), "");
        return RspVo.getSuccessResponse();
    }

    /**
     * 删除人员就职信息
     * @param staffOfficeVo
     * @return
     * @see
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/deleteOffice")
    public @ResponseBody RspVo<StaffOfficeVo> deleteOffice(String id){
        if (StringUtils.isEmpty(id)){
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
        }
        staffOfficeService.deleteByPrimaryKey(id);
        return RspVo.getSuccessResponse();
    }
}
